<template>
  <div>
    <FormPanel ref="formpanel" :list="formList2" labelWidth="100" @formChange="formchangeFuns" />
    <FilterTable
      ref="basetables"
      :columns="columns1"
      :dataSource="tablelist"
      :datakey="'rows'"
      :onColumnsChange="columns1 => (this.columns1 = columns1)"
      :height="'200'"
      :onSyncTableData="tabledataFun"
      columnsRef="zjwxclemithaving"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9999,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirmHandle">添 加</el-button>
    </div>
  </div>
</template>
<script>
import { getstorelistList, getbrandsdictList, getpartInfosList, getcplistList, getaddPartList, getqueryFeeList, getqueryInsteadPartList } from '@/api/repair/business/customer/having';
export default {
  props: ['getdata'],
  data() {
    this.basetables = null;
    return {
      formList2: this.createFroms(),
      fetchParams: {
        xhrAbort: true,
        sort: '',
        order: 'asc',
        limit: '10',
        offset: '0',
        queryPart_table: '',
        dmsFuncId: '35100010',
        _: '1565683718279'
      },
      getutildata: {
        dmsFuncId: '35100010',
        _: '1565683718279'
      },
      columns1: this.createTables(),
      tablelist: [],
      fromData: {},
      tabledata: {}
    };
  },
  created() {
    this.getlist();
    this.initdata();
  },
  mounted() {
    this.basetables = this.$refs.basetables.$refs.pageTable;
    let json = this.getdata;
    this.basetables.EXECUTE_INSERT(json);
  },
  methods: {
    createFroms() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          itemList: [],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'PART_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '名称',
          fieldName: 'PART_NAME',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '主要备件',
          fieldName: 'IS_MAIN_PART',
          itemList: [
            { text: '是', label: '1' },
            { text: '否', label: '2' }
          ]
        },
        {
          type: 'SELECT',
          label: '价格类型',
          fieldName: 'priceType2',
          itemList: this.pricelist()
        },
        {
          type: 'INPUT',
          label: '价格系数',
          fieldName: 'PRICE_RATE',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '收费区分',
          fieldName: 'CHARGE_PARTITION_CODE',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '维修项目',
          fieldName: 'LABOUR_CODE',
          itemList: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '发料单价',
          fieldName: 'CLAIM_PRICE',
          initialValue: '',
          change: val => {
            this.changemoney(val);
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '发料数量',
          fieldName: 'MIN_PACKAGE',
          initialValue: '1',
          change: val => {
            this.changemNum(val);
          }
        },
        {
          type: 'INPUT',
          label: '发料金额',
          fieldName: 'dollce',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'REMARK',
          disabled: true
        }
      ];
    },
    createTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '销售价',
          dataIndex: 'SALES_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.SALES_PRICE}</span>;
          }
        },
        {
          title: '最低销售限价',
          dataIndex: 'MIN_LIMIT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MIN_LIMIT_PRICE}</span>;
          }
        },
        {
          title: '最高销售限价',
          dataIndex: 'LIMIT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LIMIT_PRICE}</span>;
          }
        },
        {
          title: '终端建议零售价格',
          dataIndex: 'INSURANCE_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.INSURANCE_PRICE}</span>;
          }
        },
        {
          title: '建议销售价',
          dataIndex: 'INSTRUCT_PRICE',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.INSTRUCT_PRICE}</span>;
          }
        },
        {
          title: '最大库存',
          dataIndex: 'MAX_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MAX_STOCK}</span>;
          }
        },
        {
          title: '最小库存',
          dataIndex: 'MIN_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.MIN_STOCK}</span>;
          }
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.STOCK_QUANTITY}</span>;
          }
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.USEABLE_STOCK}</span>;
          }
        },
        {
          title: '借进数量',
          dataIndex: 'BORROW_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.BORROW_QUANTITY}</span>;
          }
        },
        {
          title: '借出数量',
          dataIndex: 'LEND_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LEND_QUANTITY}</span>;
          }
        },
        {
          title: '锁定量',
          dataIndex: 'LOCKED_QUANTITY',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.LOCKED_QUANTITY}</span>;
          }
        },
        {
          title: '替代备件',
          dataIndex: 'OPTION_NO',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.OPTION_NO}</span>;
          }
        },
        {
          title: '备件类别',
          dataIndex: 'leibie',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.leibie}</span>;
          }
        },
        {
          title: '单位',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.UNIT_NAME}</span>;
          }
        },
        {
          title: '车型',
          dataIndex: 'PART_MODEL_GROUP_CODE_SET',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.PART_MODEL_GROUP_CODE_SET}</span>;
          }
        }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      // this.formList1[0].itemList = (await getstorelistList(this.getutildata)).map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      // this.formList1[1].itemList = (await getbrandsdictList(this.getutildata)).map(x => ({ text: x.BRAND_CODE, value: x.BRAND_NAME }));
      // this.formList1[2].itemList = (await getpartInfosList(this.getutildata)).map(x => ({ text: x.PART_MODEL_GROUP_NAME, value: x.PART_MODEL_GROUP_CODE }));
      this.formList2[6].itemList = (await getcplistList(this.getutildata)).map(x => ({ text: x.CHARGE_PARTITION_NAME, value: x.CHARGE_PARTITION_CODE }));
    },
    formchangeFun(val) {
      console.log(val);
      this.fetchParams = Object.assign(this.fetchParams, val, { xhrAbort: false });
    },
    formchangeFuns(val) {
      this.fromData = { ...val };
      console.log(val);
    },
    initdata() {
      let json = this.getdata;
      console.log(json, 'json');

      let len = this.formList2.length;
      for (let i in json) {
        for (let j = 0; j < len; j++) {
          if (i == this.formList2[j].fieldName) {
            this.formList2[j].initialValue = json[i];
          }
        }
      }
    },
    // 价格类型数据字典
    pricelist() {
      return [
        {
          label: '12361001',
          text: '成本价'
        },
        {
          label: '12361002',
          text: '销售价'
        },
        {
          label: '12361003',
          text: '索赔价'
        },
        {
          label: '12361004',
          text: '网点价'
        },
        {
          label: '12361005',
          text: '最新进货价'
        },
        {
          label: '12361006',
          text: '含税成本价'
        },
        {
          label: '12361007',
          text: '终端建议零售价格'
        },
        {
          label: '12361008',
          text: '建议销售价'
        }
      ];
    },
    changemoney(val) {
      console.log(val, 'input');
      let num = this.formList2[9].initialValue || 1;
      this.formList2[10].initialValue = Number(val) * Number(num);
    },
    changemNum(val) {
      console.log(val, '改变');
      let money = this.formList2[8].initialValue || 1;
      this.formList2[10].initialValue = Number(val) * Number(money);
    },
    tabledataFun(val) {
      if (val && val.length > 0) {
        this.tabledata = val[0];
      }
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('close', false);
    },
    // 添加事件提交表单
    confirmHandle() {
      this.$refs.formpanel.SUBMIT_FORM();
      let json = Object.assign({}, this.tabledata, this.fromData);
      json.index = this.getdata.index;
      json.$index = this.getdata.$index;
      this.$emit('changedata', json);
    }
  }
};
</script>
